#include <iostream>
#include <queue>
#include <vector>
using namespace std;
/*5 5
1 1 5 5
.....
****.
.....
**.**
.....
*/
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };

int main() {
    int n, m, x1, y1, x2, y2;
    cin >> n >> m >> x1 >> y1 >> x2 >> y2;

    x1--, y1--, x2--, y2--;
    vector<vector<bool>> vis(n, vector<bool>(m));
    vector<string> grip(n);
    for (int i = 0; i < n; i++) {
        cin >> grip[i];
    }

    if (grip[x2][y2] == '*') {
        cout << -1 << endl;
        return 0;
    }

    int cnt = 0;
    queue<pair<int, int>> q;
    q.push({ x1, y1 });
    while (q.size()) {

        int k = q.size();
        cnt++;
        while (k--) {
            auto p = q.front();
            q.pop();
            int a = p.first, b = p.second;
            vis[a][b] = true;
            for (int i = 0; i < 4; i++) {
                int x = a + dx[i];
                int y = b + dy[i];
                if (x == x2 && y == y2) {
                    cout << cnt << endl;
                    return 0;
                }
                if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && grip[x][y] == '.') {
                    q.push({ x, y });
                }
            }
        }

    }
    cout << -1 << endl;
    return 0;
}